#include<bits/stdc++.h>
using namespace std;
int n,ans=15;
struct node{
	int x1,y1,x2,y2,w,s[18],t;
	bool vis;
}a[15];
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))
	{
		f=(ch=='-')?-f:f;
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
bool check(int r)
{
	for(int i=1;i<=a[r].t;i++) 
		if(!a[a[r].s[i]].vis)
			return 0;
	return 1;
}
void dfs(int x,int s,int last)
{
	if(ans<=x) return;
	if(s==n)
	{
		ans=min(ans,x);
		return;
	}
	for(int i=1;i<=n;++i)
	{
		if(!a[i].vis&&(check(i)||!a[i].x1))
		{
			a[i].vis=1;
			dfs(x+(last!=a[i].w),s+1,a[i].w);
			a[i].vis=0;
		}
	}
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%d%d%d%d%d",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2,&a[i].w);
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			if(i!=j && a[i].x1==a[j].x2 && !(a[j].y2<a[i].y1 || a[j].y1>a[i].y2)) 
				a[i].s[++a[i].t]=j;
	dfs(0,0,0);
	printf("%d\n",ans);
	return 0;
}
